#include <stdio.h> 
#include <pwd.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h> 
#include <string.h>
#include <sys/types.h> 
#include <dirent.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "Global.h"
#include "ScanDisk.h"
#include <string>
#include <pthread.h>

#include "Db.h"

char*  *ScanFolderSet;


/**********************************************
* ������: ScanDirectory()                     *
* ��  ��: ɨ��Ӳ��ָ��Ŀ¼�������ļ�����Ŀ¼, *
*         ���ļ���Ϣд�뵽�ļ�                *
***********************************************/
void* ScanDirectory(void* arg)
{
	int		id;
	FILE*	fpTextFile;
	DIR*	handle;
	DIR*	handle1;
	char**	FolderSet;
	int		iStackTop;
	int		iFolderSetSize;

	char	mode[4];
	char	filename[255];
	char	CurrentFile[2000];
	char	CurrentFolder[2000];

	struct stat	   buf;
	struct dirent*	direntp; 
 
	id=*(int*)arg;

	sprintf(filename, "/data/data/com.example.anything/File%d.dat", id);
	fpTextFile=fopen(filename, "wb");
	if (fpTextFile==NULL)
	{
		printf("���ļ�ʧ�ܣ����������������ϵ���쳣��Ϊ�ļ� %s �ĵ� %d ��\n", __FILE__,__LINE__);
		exit(-1);
	}
	if (strcmp(ScanFolderSet[id],"")==0)
	{
		return NULL;
	}

	iFolderSetSize=1000;
	FolderSet=(char**)malloc(iFolderSetSize*(sizeof(char*)));
	
	iStackTop=1;
	memset(mode, 0, 4);
	FolderSet[0]=new char[2000];
	strcpy(FolderSet[0], ScanFolderSet[id]);

	while (iStackTop!=0)
	{
		iStackTop--;
		strcpy(CurrentFolder, FolderSet[iStackTop]);
		delete(FolderSet[iStackTop]);

		handle=opendir(CurrentFolder);
		if( handle==NULL )
		{    
			closedir(handle);
			continue;
		}
		else
		{
			direntp=readdir(handle);
			while( direntp !=NULL )
			{     
				if( direntp->d_name[0]=='.' )
				{	
					direntp=readdir(handle);
					continue;
				}
				sprintf(CurrentFile, "%s/%s", CurrentFolder, direntp->d_name);
				lstat(CurrentFile,&buf);
				if (S_ISLNK(buf.st_mode))
				{
					handle1 = NULL;
				}
				else
				{
					handle1=opendir(CurrentFile);
				}
				if (handle1==NULL)
				{
					//This is a file
//					closedir(handle1);//This must be error
					fprintf(fpTextFile, "_%d %d\t%s\t%s\t%d\t%d\n", buf.st_mode,buf.st_uid,  direntp->d_name, CurrentFolder,buf.st_size, buf.st_mtime);
				}else
				{
					//This is a folder
					closedir(handle1);
					fprintf(fpTextFile, "d%d %d\t%s\t%s\t0\t%d\n",  buf.st_mode,buf.st_uid, direntp->d_name, CurrentFolder, buf.st_mtime);

					FolderSet[iStackTop]=new char[2000];
					strcpy(FolderSet[iStackTop], CurrentFile);
					iStackTop++;
					if (iStackTop>=iFolderSetSize)
					{
						iFolderSetSize*=2;
						FolderSet=(char**)realloc(FolderSet, iFolderSetSize*(sizeof(char*)));
					}
				}
				
				direntp=readdir(handle);	
			}
			closedir(handle);

		}

	}

	fclose(fpTextFile);

	return NULL;
}


/*******************************************
* ������: ScanDisk()                       *
* ��  ��: ��                               *
* ��  ��: ɨ��Ӳ�̸�ָ��Ŀ¼��ÿ��Ŀ¼��   *
*         ���ļ�ϵͳ���浽ָ���ļ����ٺϲ� *
********************************************/
int  CScanDisk::ScanDisk()
{
	FILE*	fp;
	FILE*	fpAllFile;
	int     i,  j;
	int		count;
	size_t	result;
	char*	buff;
	char	filename[300];
	int     length;
	pthread_t thread[100]; 

	char* filePath = "/data/data/com.example.anything/ScanDirectories.txt";
	fp=fopen(filePath, "rb");
	if (fp==NULL)
	{
		printf("ϵͳ�ļ���ʧ�����뿪������ϵ!�쳣��Ϊ�ļ� %s �ĵ� %d ��!\n",__FILE__,__LINE__);
		exit(-1);
	}

	CDb* DbHandle = new CDb;
	DbHandle->OpenDb();

	count=0;
	printf("����ɨ��Ӳ���ļ�ϵͳ���������Ҫ�����ӣ����Ժ�......\n");
	ScanFolderSet=(char*  *)malloc(100*sizeof(char*));
	for (i=0; i<100; i++)
	{
		//��ȡ��ǰɨ��Ŀ¼ʧ��
		if(fgets(filename, 300, fp)==NULL)
		{
			fclose(fp);
			break;
		}

		//��ǰ��Ϊע��
		if (filename[0]!='/')
		{
			continue;
		}

		//��ȡ��һ������Ŀ¼
		j=0;
		while (filename[j]!=10 && filename[j]!=13 &&j<300)
		{
			j++;
		}
		filename[j]=0;
		if(strlen(filename)==0)
		{
			continue;
		}

		//���һ��ɨ�����
		ScanFolderSet[count]=new char[300];
		memset(ScanFolderSet[count], 0, 300);
		strcpy(ScanFolderSet[count],filename);
		printf("�� %d ��ɨ����У�%s .....\n",count, ScanFolderSet[count]);
		if ( strlen(ScanFolderSet[count])==0 )
		{
			break;
		}

		int *arg;
		arg = (int*)malloc(sizeof(int));
		*arg = count;

		//����һ���߳�ɨ���Ŀ¼
		result=pthread_create(&thread[count], NULL, ScanDirectory, (void*)arg);
//		ScanDirectory((void*)count);
		if(result!=0)
		{  
			printf("�����߳��쳣!\n");
			return -3;
		}

		count++;
		if (count>99)
		{
			printf("����ɨ��Ŀ¼���ܳ���100����gserach��ֻ��ǰ100��ɨ��Ŀ¼���?");
			fclose(fp);
			break;
		}
	}


	//�ȴ������߳�ɨ��Ӳ�̽���
	for (j=0; j<count; j++ )
	{
		pthread_join(thread[j],NULL);
	}

	fpAllFile=fopen("/data/data/com.example.anything/AllFile.dat","wb");
	if (fpAllFile==NULL)
	{
		printf("���ļ�ʧ�ܣ����뿪������ϵ!�쳣��Ϊ�ļ� %s �ĵ� %d �У�\n", __FILE__,__LINE__);
		exit(-3);
	}

	//����Ŀ¼ɨ�赽���ļ�ϵͳ�ϲ�
	for(i=0; i<100; i++)
	{		
		sprintf(filename,"/data/data/com.example.anything/File%d.dat",i);
		fp=fopen(filename, "rb");
		if(fp==NULL)
		{
			break;
		}

		fseek(fp, 0L, SEEK_END);
		length = ftell(fp);

		buff=new char[length+1];
		fseek(fp, 0L, SEEK_SET);
		fread (buff, length, 1, fp);
		fwrite(buff, length, 1, fpAllFile);
		fclose(fp);
		delete(buff);
	}

	fclose(fpAllFile);


	return 0;

}



